<?php
require_once(WCF_DIR.'lib/system/event/EventListener.class.php');

/**
 *  Event Listener for the index page to insert the users absent list.
 *
 * @author      Sven Kutzner
 * @copyright	2007-2009 deixu.net
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	net.deixu.mfcfb.user.absence
 */
class IndexPageAbsenceListener implements EventListener {
        /**
         * @see EventListener::execute()
         */
	public function execute($eventObj, $className, $eventName) {
		// exit if there is nothing to display
		if(!MODULE_ABSENCE || !INDEX_ENABLE_ABSENCELIST || !WCF::getUser()->getPermission('user.profile.absence.canSeeMessages'))
			return;

		$usersAbsent = array();
		try {
			$isAbsent = 'userOption'.User::getUserOptionID('isAbsent');
			$absenceStart = 'userOption'.User::getUserOptionID('absenceStart');
			$absenceEnd = 'userOption'.User::getUserOptionID('absenceEnd');
			if(!OPTION_TIMESTAMP_WITH_TIME) $absenceEnd .= '+'.AbsenceUtil::SEC_DAY;
			$invisible = 'userOption'.User::getUserOptionID('invisible');

			$sql = 'SELECT user_option.'.$invisible.', user_option.'.$isAbsent.',
					user_option.'.$absenceStart.', user_option.'.$absenceStart.',
					groups.userOnlineMarking, user.username, user.userID
					FROM 		wcf'.WCF_N.'_user user
					LEFT JOIN 	wcf'.WCF_N.'_user_option_value user_option
					ON		(user_option.userID = user.userID)
					LEFT JOIN 	wcf'.WCF_N.'_group groups
					ON		(groups.groupID = user.userOnlineGroupID)
					WHERE user_option.'.$isAbsent.'=\'1\'
					AND user_option.'.$absenceStart.' <= "'.TIME_NOW.'"
					AND user_option.'.$absenceEnd.' >= "'.TIME_NOW.'"
					ORDER BY user.username';
			if($result = WCF::getDB()->sendQuery($sql)) {
				while($row = WCF::getDB()->fetchArray($result))
					$usersAbsent[$row['userID']] = $row;
			}
		}
		catch(Exception $e) { }

		if(count($usersAbsent)) {
			WCF::getTPL()->assign(array('usersAbsent'=>$usersAbsent));
			WCF::getTPL()->append('additionalBoxes2', WCF::getTPL()->fetch('absenceList'));
		}
		
	}
}
?>